% html: Beginning of file: `contribute.html'
%  Contributing to the Go project 
\chapter{How to contribute}
\label{chap:contribute}

\section{Introduction}


This document explains how to write a new package,
how to test code, and how to contribute changes to the Go project.
It assumes you have installed Go using the
installation instructions.  (Note that
the \texttt{gccgo} frontend lives elsewhere;
see Contributing to gccgo.)

Before embarking on a significant change to an existing
package or the creation of a major new package,
it's a good idea to send mail to the mailing list\footnote{See URL http://groups.google.com/group/golang-nuts}
to let people know what you are thinking of doing.
Doing so helps avoid duplication of effort and
enables discussions about design before much code
has been written.

\section{Community resources}


For real-time help, there may be users or developers on
\texttt{\#go-nuts} on the Freenode\footnote{See URL http://freenode.net/} IRC server.

The official mailing list for discussion of the Go language is
Go Nuts\footnote{See URL http://groups.google.com/group/golang-nuts}.

Bugs can be reported using the Go issue tracker\footnote{See URL http://code.google.com/p/go/issues/list}.

For those who wish to keep up with development,
there is another mailing list, golang-checkins\footnote{See URL http://groups.google.com/group/golang-checkins},
that receives a message summarizing each checkin to the Go repository.

\section{Creating a new package}


The source code for the package with import path
\texttt{x/y} is, by convention, kept in the
directory \texttt{\$GOROOT/src/pkg/x/y}.

\subsection{Makefile}


It would be nice to have Go-specific tools that
inspect the source files to determine what to build and in
what order, but for now, Go uses GNU \texttt{make}.
Thus, the first file to create in a new package directory is
usually the \texttt{Makefile}.
The basic form is illustrated by \texttt{src/pkg/container/vector/Makefile}:
\begin{verbatim} 
include $(GOROOT)/src/Make.$(GOARCH)

TARG=container/vector
GOFILES=\
	intvector.go\
	stringvector.go\
	vector.go\

include $(GOROOT)/src/Make.pkg
\end{verbatim}

The first and last lines \texttt{include} standard definitions and rules,
so that the body of the \texttt{Makefile} need only specify two variables.

\texttt{TARG} is the target install path for the package,
the string that clients will use to import it.
This string should be the same as the directory
in which the \texttt{Makefile} appears, with the
\texttt{\$GOROOT/src/pkg/} removed.

\texttt{GOFILES} is a list of source files to compile to
create the package.  The trailing \texttt{\mbox{$\backslash$}} characters
allow the list to be split onto multiple lines
for easy sorting.

After creating a new package directory, add it to the list in
\texttt{\$GOROOT/src/pkg/Makefile} so that it
is included in the standard build.  Then run:
\begin{verbatim} 
cd $GOROOT/src/pkg
./deps.bash
\end{verbatim}

to update the dependency file \texttt{Make.deps}.

If you change the imports of an existing package,
you do not need to edit \texttt{\$GOROOT/src/pkg/Makefile}
but you will still need to run \texttt{deps.bash} as above.

\subsection{Go source files}


The first statement in each of the source files listed in the \texttt{Makefile}
should be \texttt{package \textit{name}}, where \texttt{\textit{name}}
is the package's default name for imports.
(All files in a package must use the same \texttt{\textit{name}}.)
Go's convention is that the package name is the last element of the
import path: the package imported as \texttt{{\tt{}"{}}crypto/rot13{\tt{}"{}}}
should be named \texttt{rot13}.
The Go tools impose a restriction that package names are unique
across all packages linked into a single binary, but that restriction
will be lifted soon.

Go compiles all the source files in a package at once, so one file
can refer to constants, variables, types, and functions in another
file without special arrangement or declarations.

Writing clean, idiomatic Go code is beyond the scope of this document.
Effective Go is an introduction to
that topic.

\section{Testing}


Go has a lightweight test framework known as \texttt{gotest}.
You write a test by creating a file with a name ending in \texttt{\_test.go}
that contains functions named \texttt{TestXXX} with signature \texttt{func (t *testing.T)}.
The test framework runs each such function;
if the function calls a failure function such as \texttt{t.Error} or \texttt{t.Fail}, the test is considered to have failed.
The gotest command documentation
and the testing package documentation give more detail.

The \texttt{*\_test.go} files should not be listed in the \texttt{Makefile}.

To run the test, run either \texttt{make test} or \texttt{gotest}
(they are equivalent).
To run only the tests in a single test file, for instance \texttt{one\_test.go},
run \texttt{gotest one\_test.go}.

Before sending code out for review, make sure everything
still works and the dependencies are right:
\begin{verbatim} 
cd $GOROOT/src
./all.bash
\end{verbatim}

The final line printed by \texttt{all.bash} should be of the form:
\begin{verbatim} 
N known bugs; 0 unexpected bugs
\end{verbatim}

The value of \textit{N} varies over time, but the line must
say ``\texttt{0 unexpected bugs}'' and must not
add ``\texttt{test output differs}.''

Once your new code is tested and working,
it's time to get it reviewed and submitted.

\section{Code review}


Changes to Go must be reviewed before they are submitted,
no matter who makes the change.
(In exceptional cases, such as fixing a build, the review can
follow shortly after submitting.)
A Mercurial extension helps manage the code review process.
The extension is included in the Go source tree but needs
to be added to your Mercurial configuration.

\subsection{Caveat for Mercurial aficionados}


\textit{Using Mercurial with the code review extension is not the same
as using standard Mercurial.}

The Go repository is maintained as a single line of reviewed changes;
we prefer to avoid the complexity of Mercurial's arbitrary change graph.
The code review extension helps here: its \texttt{hg submit} command
automatically checks for and warns about the local repository
being out of date compared to the remote one.
The \texttt{hg submit} command also verifies other
properties about the Go repository.
For example,
it checks that Go code being checked in is formatted in the standard style,
as defined by gofmt,
and it checks that the author of the code is properly recorded for
copyright purposes.

To help ensure changes are only created by \texttt{hg submit},
the code review extension disables the standard \texttt{hg commit}
command.

Mercurial power users: To allow Go contributors to take advantage of
Mercurial's functionality for local revision control, it might be interesting
to explore how the code review extension can be made to work alongside
the Mercurial Queues extension.

\subsection{Configure the extension}


Edit \texttt{\$GOROOT/.hg/hgrc} to add:
\begin{verbatim} 
[extensions]
codereview = YOUR_GO_ROOT/lib/codereview/codereview.py
\end{verbatim}

Replace YOUR\_GO\_ROOT with the value of \texttt{\$GOROOT}.
The Mercurial configuration file format does not allow environment variable substitution.

\subsection{Log in to the code review site.}


The code review server uses a Google Account to authenticate.
(If you can use the account to
sign in at google.com,
you can use it to sign in to the code review server.)
\begin{verbatim} 
$ cd $GOROOT
$ hg code-login
Email (login for uploading to codereview.appspot.com): rsc@golang.org
Password for rsc@golang.org:

Saving authentication cookies to /Users/rsc/.codereview_upload_cookies_codereview.appspot.com
\end{verbatim}

\subsection{Configure your account settings.}


Edit your code review settings\footnote{See URL http://codereview.appspot.com/settings}.
Grab a nickname.
Many people prefer to set the Context option to
``Whole file'' to see more context when reviewing changes.

Once you have chosen a nickname in the settings page, others
can use that nickname as a shorthand for naming reviewers and the CC list.
For example, \texttt{rsc} is an alias for \texttt{rsc{\char64}golang.org}.

\subsection{Make a change}


The entire checked-out tree is writable.
If you need to edit files, just edit them: Mercurial will figure out which ones changed.
You do need to inform Mercurial of added, removed, copied, or renamed files,
by running
\texttt{hg add},
\texttt{hg rm},
\texttt{hg cp},
or
\texttt{hg mv}.

When you are ready to send a change out for review, run
\begin{verbatim} 
$ hg change
\end{verbatim}

from any directory in your Go repository.
Mercurial will open a change description file in your editor.
(It uses the editor named by the \texttt{\$EDITOR} environment variable, \texttt{vi} by default.)
The file will look like:
\begin{verbatim} 
# Change list.
# Lines beginning with # are ignored.
# Multi-line values should be indented.

Reviewer:
CC:

Description:
	<enter description here>

Files:
	src/pkg/math/sin.go
	src/pkg/math/tan.go
	src/pkg/regexp/regexp.go
\end{verbatim}

The \texttt{Reviewer} line lists the reviewers assigned
to this change, and the \texttt{CC} line lists people to
notify about the change.
These can be code review nicknames or arbitrary email addresses.

Replace ``\texttt{\mbox{$<$}enter description here\mbox{$>$}}''
with a description of your change.
The first line of the change description is conventionally
a one-line summary of the change and is used as the
subject for code review mail; the rest of the
description elaborates.

The \texttt{Files} section lists all the modified files
in your client.
It is best to keep unrelated changes in different change lists.
In this example, we can include just the changes to package \texttt{math}
by deleting the line mentioning \texttt{regexp.go}.

After editing, the template might now read:
\begin{verbatim} 
# Change list.
# Lines beginning with # are ignored.
# Multi-line values should be indented.

Reviewer: r, rsc
CC: math-nuts@swtch.com

Description:
	Sin, Cos, Tan: improved precision for very large arguments

	See Bimmler and Shaney, ``Extreme sinusoids,'' J. Math 3(14).
	Fixes issue 159.

Files:
	src/pkg/math/sin.go
	src/pkg/math/tan.go
\end{verbatim}

The special sentence ``Fixes issue 159.'' associates
the change with issue 159 in the Go issue tracker\footnote{See URL http://code.google.com/p/go/issues/list}.
When this change is eventually submitted, the issue
tracker will automatically mark the issue as fixed.

Save the file and exit the editor.

The code review server assigns your change an issue number and URL,
which \texttt{hg change} will print, something like:
\begin{verbatim} 
CL created: http://codereview.appspot.com/99999
\end{verbatim}

If you need to re-edit the change description,
run \texttt{hg change 99999}.

You can see a list of your pending changes by running \texttt{hg pending} (\texttt{hg p} for short).

\subsection{Synchronize your client}


While you were working, others might have submitted changes
to the repository.  To update your client, run
\begin{verbatim} 
$ hg sync
\end{verbatim}

(For Mercurial fans, \texttt{hg sync} runs \texttt{hg pull -u}
but then also synchronizes the local change list state against the new data.)

If files you were editing have changed, Mercurial does its best to merge the
remote changes into your local changes.  It may leave some files to merge by hand.

For example, suppose you have edited \texttt{flag\_test.go} but
someone else has committed an independent change.
When you run \texttt{hg sync}, you will get the (scary-looking) output
(emphasis added):
\begin{verbatim} 
$ hg sync
adding changesets
adding manifests
adding file changes
added 1 changeset with 2 changes to 2 files
getting src/pkg/flag/flag.go
couldn't find merge tool hgmerge
merging src/pkg/flag/flag_test.go
warning: conflicts during merge.
merging src/pkg/flag/flag_test.go failed!
1 file updated, 0 files merged, 0 files removed, 1 file unresolved
use 'hg resolve' to retry unresolved file merges
$
\end{verbatim}

The only important part in that transcript is the italicized line:
Mercurial failed to merge your changes with the independent change.
When this happens, Mercurial leaves both edits in the file,
marked by \texttt{\mbox{$<$}\mbox{$<$}\mbox{$<$}\mbox{$<$}\mbox{$<$}\mbox{$<$}\mbox{$<$}} and
\texttt{\mbox{$>$}\mbox{$>$}\mbox{$>$}\mbox{$>$}\mbox{$>$}\mbox{$>$}\mbox{$>$}}.
it is now your job to edit the file to combine them.
Continuing the example, searching for those strings in \texttt{flag\_test.go}
might turn up:
\begin{verbatim} 
	VisitAll(visitor);
<<<<<<< local
	if len(m) != 7 {
=======
	if len(m) != 8 {
>>>>>>> other
		t.Error("VisitAll misses some flags");
\end{verbatim}

Mercurial doesn't show it, but suppose the original text that both edits
started with was 6; you added 1 and the other change added 2,
so the correct answer might now be 9.  If you edit the section
to remove the markers and leave the correct code:
\begin{verbatim} 
	VisitAll(visitor);
	if len(m) != 9 {
		t.Error("VisitAll misses some flags");
\end{verbatim}

then that is enough.  There is no need to inform Mercurial
that you have corrected the file.

If you had been editing the file, say for debugging, but do not
care to preserve your changes, you can run
\texttt{hg revert flag\_test.go} to abandon your
changes.

\subsection{Mail the change for review}


To send out a change for review, run \texttt{hg mail} using the change list number
assigned during \texttt{hg change}:
\begin{verbatim} 
$ hg mail 99999
\end{verbatim}

You can add to the \texttt{Reviewer:} and \texttt{CC:} lines
using the \texttt{-r} or \texttt{--cc} options.
In the above example, we could have left the \texttt{Reviewer} and \texttt{CC}
lines blank and then run:
\begin{verbatim} 
$ hg mail -r r,rsc --cc math-nuts@swtch.com 99999
\end{verbatim}

to achieve the same effect.

Note that \texttt{-r} and \texttt{--cc} cannot be spelled \texttt{--r} or \texttt{-cc}.

\subsection{Reviewing code}


Running \texttt{hg mail} will send an email to you and the reviewers
asking them to visit the issue's URL and make coments on the change.
When done, the reviewer clicks ``Publish and Mail comments''
to send comments back.

\subsection{Revise and upload}


You will probably revise your code in response to the reviewer comments.
When you have revised the code and are ready for another round of review, run
\begin{verbatim} 
$ hg upload 99999
\end{verbatim}

to upload the latest copy.
You might also visit the code review web page and reply to the comments,
letting the reviewer know that you've addressed them or explain why you
haven't.  When you're done replying, click ``Publish and Mail comments''
to send the line-by-line replies and any other comments.
A common acronym in such mails is \texttt{PTAL}: please take another look.

The reviewer can comment on the new copy, and the process repeats.
The reviewer approves the change by replying with a mail that says
\texttt{LGTM}: looks good to me.

\subsection{Submit the change}


Once the code has been \texttt{LGTM}'ed, it is time to submit
it to the Mercurial repository.
If you are a committer, you can run:
\begin{verbatim} 
$ hg submit 99999
\end{verbatim}

This checks the change into the repository.
The change description will include a link to the code review,
and the code review will be updated with a link to the change
in the repository.

If your local copy of the repository is out of date,
\texttt{hg submit}
will refuse the change:
\begin{verbatim} 
$ hg submit 12345678
local repository out of date; must sync before submit
\end{verbatim}

If you are not a committer, you cannot submit the change directly.
Instead, a committer, usually the reviewer who said \texttt{LGTM},
will run:
\begin{verbatim} 
$ hg clpatch 99999
$ hg submit 99999
\end{verbatim}

The \texttt{clpatch} command imports your change 99999 into
the committer's local Mercurial client, at which point the committer
can check or test the code more.
(Anyone can run \texttt{clpatch} to try a change that
has been uploaded to the code review server.)
The \texttt{submit} command submits the code.  You will be listed as the
author, but the change message will also indicate who the committer was.
Your local client will notice that the change has been submitted
when you next run \texttt{hg sync}.

\subsection{Copyright}


The standard copyright header for files in the Go tree is:
\begin{verbatim} 
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
\end{verbatim}

Code you contribute should have this header.
You need to be listed in the
\texttt{CONTRIBUTORS} file,
which defines who the Go contributors---the people---are;
and the copyright holder for the code you submit (either you or the
organization you work for) needs to be listed in the
\texttt{AUTHORS} file, which defines
who ``The Go Authors''---the copyright holders---are.

When sending your first change list, you should prepare
and send a separate change list adding yourself to
\texttt{CONTRIBUTORS} and adding
the copyright holder for your code to \texttt{AUTHORS} if not already listed.
If you are the copyright holder, you will need to agree to
the individual contributor license agreement\footnote{See URL http://code.google.com/legal/individual-cla-v1.0.html},
which can be completed online;
if your organization is the copyright holder, the organization
will need to agree to the corporate contributor license agreement\footnote{See URL http://code.google.com/legal/corporate-cla-v1.0.html}.
If the copyright holder for your code has already completed the
agreement in connection with another Google open source project,
it does not need to be completed again.
One of the Go developers at Google will approve and submit
this change after checking the list of people/organizations
that have completed the agreement.
% html: End of file: `contribute.html'
